﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OurFAS.Data;
using System.Transactions;

namespace OurFAS.MySystem
{
    public partial class FrmRole : Form
    {
        #region 成员变量
        private string _sysRoleCode = "01";
        #endregion

        #region 构造函数
        public FrmRole()
        {
            InitializeComponent();
        }
        #endregion

        #region 控件函数
        private void FrmRole_Load(object sender, EventArgs e)
        {
            RefreshRoleList();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            FrmRoleDetail window = new FrmRoleDetail(Data.EnumHelper.EnumObjctOperateType.New, null);
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                RefreshRoleList();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void dgv_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Edit();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("请选择要删除的项");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;
            if (rowIndex < 0)
            {
                return;
            }

            DataGridViewRow row = dgv.Rows[rowIndex];
            Base_Role record = row.DataBoundItem as Base_Role;
            if (record.RoleCode.Trim() == _sysRoleCode)
            {
                MessageBox.Show("系统管理员角色不能删除!");
                return;
            }

            if (MessageBox.Show("是否确认删除所选择的项？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }

            try
            {
                OurFasDBEntities entityRole = new OurFasDBEntities();
                entityRole.Base_Role.DeleteObject(record);
                OurFasDBEntities entityMenu = new OurFasDBEntities();
                List<Base_Menu> delMenuList
                          = entityMenu.Base_Menu.Where(c => c.RoleCode.Trim() == record.RoleCode).ToList();
                foreach (Base_Menu menu in delMenuList)
                {
                    entityMenu.DeleteObject(menu);
                }

                using (TransactionScope scope = new TransactionScope())
                {
                    entityRole.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
                    entityMenu.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);

                    scope.Complete();
                }
                MessageBox.Show("删除角色成功!");
                RefreshRoleList();
            }
            catch (System.Exception exc)
            {
                MessageBox.Show(this, "删除项出错，错误：" + exc.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 私有函数
        private void RefreshRoleList()
        {
            dgv.DataSource = null;
            OurFasDBEntities entity = new OurFasDBEntities();
            if (entity.Base_Role.Count() > 0)
            {
                dgv.DataSource = entity.Base_Role;
            }
        }

        private void Edit()
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("请选择要编辑的项");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            Base_Role record = row.DataBoundItem as Base_Role;
            FrmRoleDetail window = new FrmRoleDetail(Data.EnumHelper.EnumObjctOperateType.Edit, record);
            if (window.ShowDialog() == DialogResult.OK)
            {
                RefreshRoleList();
            }
        }
        #endregion

    
    }
}
